import { action, observable } from 'mobx'
import { getHotSearchList, getSearchList, getSearchTips } from '../../server/search'
import { SearchArticleItem, SearchRankItem } from '../../utils/interface'
class Search{
  //热门搜索数据
  @observable
  hotList: string[] = []
  //历史搜索数据
  @observable
  historyList: string[] = []

  @observable
  searchRankList: SearchRankItem[] = []
  @observable
  searchArticleList: SearchArticleItem[] = []
  @observable
  searchRecommendRankList: SearchRankItem[] = []
  @observable
  searchRecommendArticleList: SearchArticleItem[] = []
  
  //关键字搜索数据
  @observable
  searchTipsList: string[] = [] 
  


  //热门搜索
  @action
  async getHotList() {
    let result = await getHotSearchList()
    if (result) {
      this.hotList=result.data.dataList
    }
  }
  //搜索
  @action
  async getSearch(keyword:string) {
    let result = await getSearchList(keyword)
    console.log(result)
    if (result) {
      this.searchRankList = result.data.rankList;
      this.searchArticleList = result.data.articleList;
      console.log(this.searchArticleList)
      this.searchRecommendRankList = result.data.recommendRankList;
      this.searchRecommendArticleList = result.data.recommendArticleList;
    }
  }
  //关键字搜索
  @action
      async getSearchTips(keyword: string){
          let result = await getSearchTips(keyword);
          if (result){
              this.searchTipsList = result.data.dataList;
          }
      }
  //历史搜索
  @action
  getHistoryList(){
      let history: string | string[] = localStorage.getItem('arr');
      if (history){
        history = JSON.parse(history)
      }else{
          history = [];
      }
      this.historyList = history as string[];
  }
  //清除历史搜索
  @action
  clearHistoryList(){
      localStorage.removeItem('arr');
  }
}
export default Search;
